package com.bw.zg3_day05zy.news

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.base.entity.NewsBeanItem
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3_day05zy.R
import com.bw.zg3_day05zy.databinding.ActivityNewsBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/app/NewsActivity")
@AndroidEntryPoint
class NewsActivity : BaseActivity<ActivityNewsBinding, NewsViewModel>() {
    val newsAdapter by lazy { NewsAdapter() }
    override fun initView() {
        binding.rv.apply {
            adapter = newsAdapter
            layoutManager = LinearLayoutManager(this@NewsActivity)
        }
        newsAdapter.setOnItemClickListener(object :NewsAdapter.OnItemClickListener{
            override fun onItemClick(item: NewsBeanItem, position: Int) {
                ARouter.getInstance().build("/app/NewsDetailActivity").withInt("newsId",item.id).navigation()
            }

        })
    }

    override fun initData() {
        lifecycleScope.launch {
            viewModel.getNews().collect{
                newsAdapter.submitData(it)
            }
        }
    }

}